package com.Renderer;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.Core.GameEngine;

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

public class GameBackground
{	private Bitmap gameFieldBG, gameFieldPlay;
	private FloatBuffer vertexPlayBuffer,vertexBgBuffer;
	private FloatBuffer textureBuffer;
	private ByteBuffer indexBuffer;
	private int[] textures=new int[2];
	private float verticesBg[];
	private float verticesPlay[]=
		{	GameEngine.LOW_BOUND, GameEngine.LOW_BOUND, 0.0f,
			GameEngine.UP_BOUND	, GameEngine.LOW_BOUND, 0.0f,
			GameEngine.UP_BOUND	, GameEngine.UP_BOUND , 0.0f,
			GameEngine.LOW_BOUND, GameEngine.UP_BOUND , 0.0f
		};
	private float texture[]=
		{	0.0f, 0.0f,
			1.0f, 0.0f,
			1.0f, 1.0f,
			0.0f, 1.0f
		};
	private byte indices[]=
		{	0,1,2,
			0,2,3
		};
	
	public GameBackground()
	{	vertexPlayBuffer = PrepareBuffer(verticesPlay);
		textureBuffer = PrepareBuffer(texture);
		
		indexBuffer = ByteBuffer.allocateDirect(indices.length); 
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}
	private FloatBuffer PrepareBuffer(float[] vArray)
	{	ByteBuffer byteBuf = ByteBuffer.allocateDirect(vArray.length*4);
		byteBuf.order(ByteOrder.nativeOrder());
		FloatBuffer buf = byteBuf.asFloatBuffer();
		buf.put(vArray);buf.position(0);
		return buf;
	}
	public void SuggestOrientation()
	{	float[] vt =
			{	GameEngine.pjLeft , GameEngine.pjBottom	, 0.0f,
				GameEngine.pjRight, GameEngine.pjBottom	, 0.0f,
				GameEngine.pjRight, GameEngine.pjTop	, 0.0f,
				GameEngine.pjLeft , GameEngine.pjTop	, 0.0f,
			};
		verticesBg = vt;
		vertexBgBuffer = PrepareBuffer(verticesBg);
	}
	public void loadTexture(GL10 gl, Context context)
	{	InputStream imagestream = null;
		try
		{	imagestream 	= context.getResources().openRawResource(GameEngine.GAME_FIELD_BG);
			gameFieldBG 	= BitmapFactory.decodeStream(imagestream);
			imagestream 	= context.getResources().openRawResource(GameEngine.GAME_FIELD_PLAY);
			gameFieldPlay	= BitmapFactory.decodeStream(imagestream);
		}
		catch (Exception e)
		{}
		finally
		{	try
			{	imagestream.close();
				imagestream=null;
			}
			catch (IOException e)
			{}
		}
		
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
	
		gl.glGenTextures(2, textures, 0);
		
//		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, gameFieldBG, 0);
//		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
//		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, gameFieldPlay, 0);
		ByteBuffer bb = extract(gameFieldBG); 
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,gameFieldBG.getWidth(), gameFieldBG.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 
		
		ByteBuffer bb1 = extract(gameFieldPlay); 
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
		gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,gameFieldPlay.getWidth(), gameFieldPlay.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb1);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		
		gameFieldBG.recycle();
		gameFieldPlay.recycle();
	}
	public void draw(GL10 gl)
	{	gl.glFrontFace(GL10.GL_CCW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBgBuffer);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexPlayBuffer);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
		
		
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
	}
	
	private static ByteBuffer extract(Bitmap bmp)
	{
		ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight() * bmp.getWidth() * 4);
//		bb.order(ByteOrder.BIG_ENDIAN);
		IntBuffer ib = bb.asIntBuffer();
		// Convert ARGB -> RGBA
		for (int y = bmp.getHeight() - 1; y > -1; y--)
		{

			for (int x = 0; x < bmp.getWidth(); x++)
			{
				int pix = bmp.getPixel(x, bmp.getHeight() - y - 1);
				int alpha = ((pix >> 24) & 0xFF);
				int red = ((pix >> 16) & 0xFF);
				int green = ((pix >> 8) & 0xFF);
				int blue = ((pix) & 0xFF);

				// Make up alpha for interesting effect

				//ib.put(red << 24 | green << 16 | blue << 8 | ((red + blue + green) / 3));
				ib.put(red << 24 | green << 16 | blue << 8 | alpha);
			}
		}
		bb.position(0);
		return bb;
	} 
}


